﻿using System;
using System.Collections.Generic;
using System.Text;

namespace TestEffectiveCS
{
    //使用属性代替可访问的数据成员
    public class Test01
    {
        //属性绑定到控件上
        //当控制修改文本值时类的属性会同步修改
        //当修改类型的属性值时，控件的文件属性不会变化
        //必需调用DataBindings["Text"].ReadValue();方法
        //public class Student
        //{
        //    string _name;
        //    public string Name
        //    {
        //        get { return _name; }
        //        set { _name = value; }
        //    }
        //    public Student(string name)
        //    {
        //        this.Name = name;
        //    }
        //}
        //textBox1.DataBindings.Add("Text", _student, "Name");
        //textBox1.DataBindings["Text"].Parse += new ConvertEventHandler(Form1_Parse);
        //void Form1_Parse(object sender, ConvertEventArgs e)
        //{
        //}
        //private void textBox2_TextChanged(object sender, EventArgs e)
        //{
        //    _student.Name = (sender as TextBox).Text;
        //    this.textBox1.DataBindings["Text"].ReadValue();
        //}

        //属性的用法
        public class Custormer
        {
            private string _name;

            //基本属性
            public string Name
            {
                get { return _name; }
                set { _name = value; }
            }

            ////同步数据访问
            //public string Name
            //{
            //    get { lock (this) { return _name; } }
            //    set { lock (this) { _name = value; } }
            //}

            ////虚属性
            //public virtual string Name
            //{
            //    get { return _name; }
            //    set { _name = value; }
            //}
        }

        //抽象属性
        public interface INameValuePair
        {
            object Name { get; }
            object Value { get; set; }
        }

        //const属性和非const属性
        public interface IConstNameValuePair
        {
            object Name { get; }
            object Value { get; }
        }
        //public interface INameValuePair
        //{
        //    object Value { get; set; }
        //}
        public class Stuff : IConstNameValuePair, INameValuePair
        {
            private string _name;
            private object _value;
            #region IConstNameValuePair 成员
            public object Name
            {
                get { return _name; }
            }
            //此处的修饰符不需public
            //对于接口引用已默认是public
            //会提示：修饰符“public”对该项无效
            object IConstNameValuePair.Value
            {
                get { return _value; }
            }
            //调用接口IConstNameValuePair的Value
            //IConstNameValuePair stuff = new Stuff();
            //stuff.Value = "data"; //此处不合法
            #endregion
            #region INameValuePair 成员
            public object Value
            {
                get { return _value; }
                set { _value = value; }
            }
            //调用接口INameValuePair的Value
            //IConstNameValuePair stuff = new Stuff();
            //stuff.Value = "data"; //此处合法
            #endregion
        }

        //可见性
        public class Customer
        {
            private string _name;
            //基本属性
            public string Name
            {
                get { return _name; }
                protected set { _name = value; }
            }
        }

        //索引器
        public class MyObject
        {
            //访问方法
            //MyObject myObject = new MyObject();
            //int value = myObject[0];
            int[] _theValues1 = new int[100];
            int[,] _theValues2 = new int[100, 100];
            public int this[int index]
            {
                get { return _theValues1[index]; }
                set { _theValues1[index] = value; }
            }

            public int this[int index1, int index2]
            {
                get { return _theValues2[index1, index2]; }
                set { _theValues2[index1, index2] = value; }
            }
        }

        public Test01()
        {
            IConstNameValuePair s1 = new Stuff();
            INameValuePair s2 = new Stuff();
            //s1.Value = "aaa";
            s2.Value = "aaa";

            MyObject myObject = new MyObject();
            int value = myObject[0];
        }
    }

}
